﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Timeline.Beans;
using Timeline.Providers;
using Timeline.Utils;

namespace Timeline.Inis {
    public class Ini {
        private readonly Dictionary<string, GeneralIni> Inis = new Dictionary<string, GeneralIni>() {};
        private readonly HashSet<string> THEME = new HashSet<string>() { "", "light", "dark" };
        private string provider = "bing"; // 非null
        private string desktopProvider = ""; // 非null
        private string lockProvider = ""; // 非null
        private string theme = ""; // 非null
        private string folder = ""; // 非null

        public string User { set; get; } = "";

        public string UserEncoded() {
            return TextUtil.Encode(User);
        }

        public string Pwd { set; get; } = "";

        public string PwdEncrypted() {
            return TextUtil.Md5(Pwd);
        }

        public string Client { set; get; } = "";

        public string Provider {
            set => provider = Inis.ContainsKey(value) ? value : "bing";
            get => provider;
        }

        public string DesktopProvider {
            set => desktopProvider = Inis.ContainsKey(value) ? value : "";
            get => desktopProvider;
        }

        public string LockProvider {
            set => lockProvider = Inis.ContainsKey(value) ? value : "";
            get => lockProvider;
        }

        public string Theme {
            set => theme = THEME.Contains(value) ? value : "";
            get => theme;
        }

        public string Folder {
            //set => folder = string.Concat((value ?? "").Split(Path.GetInvalidFileNameChars()));
            set => folder = value ?? "";
            get => folder;
        }

        public int Cache { set; get; } = 600;

        public int R18 { set; get; } = 0;

        public bool SetIni(string provider, GeneralIni ini) {
            if (ini == null) {
                return false;
            }
            if (Inis.ContainsKey(provider)) {
                Inis[provider] = ini;
            } else {
                Inis.Add(provider, ini);
            }
            return true;
        }

        public GeneralIni GetIni(string provider = null) {
            return provider != null && Inis.ContainsKey(provider)
                ? Inis[provider] : Inis[this.provider];
        }

        public GeneralProvider GenerateProvider(string provider = null) {
            return provider != null && Inis.ContainsKey(provider)
                ? Inis[provider].GenerateProvider() : Inis[this.provider].GenerateProvider();
        }

        public bool ContainsProvider(string provider) {
            return provider != null && Inis.ContainsKey(provider);
        }

        override public string ToString() {
            string paras = Inis[provider].ToString();
            return $"/{Provider}?desktopprovider={DesktopProvider}&lockprovider={LockProvider}"
                + $"&user=${UserEncoded()}&theme={Theme}&folder={Folder}&cache={Cache}&r18={R18}"
                + (paras.Length > 0 ? "&" : "") + paras;
        }
    }
}
